package cn.edu.ctbu.labtest.service.impl;

import cn.edu.ctbu.labtest.dao.TeacherRepository;
import cn.edu.ctbu.labtest.domain.Teacher;
import cn.edu.ctbu.labtest.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherImplService implements TeacherService {
    @Autowired
    private TeacherRepository teacherRepository;

    /**
     * 只分页的查询
     * @param pageable
     * @return
     */
    public Page<Teacher> findAll(Pageable pageable){
        return teacherRepository.findAll(pageable);
    }

    /**
     * 分页，带过滤的查询
     */
    public Page<Teacher> findAll(Example<Teacher> teacher, Pageable pageable){
        return teacherRepository.findAll(teacher,pageable);
    }

    /**
     * 读取所有的学生数据
     * @return
     */
    public List<Teacher> findAll(){
        return teacherRepository.findAll();
    }

    /**
     * 更新教师信息
     * @param teacher
     * @return
     */
    public Teacher update(Teacher teacher){
        return teacherRepository.save(teacher);
    }

    /**
     * 新增教师信息
     * @param teacher
     * @return
     */
    public Teacher insert(Teacher teacher){
        return teacherRepository.save(teacher);
    }

    /**
     * 通过id查找教师
     * @param id
     * @return
     */
    public Teacher getById(Integer id){
        Teacher teacher=teacherRepository.findById(id).orElse(null);
        return teacher;
    }

    /**
     * 通过名字查询教师
     * @param name
     * @return
     */
    public List<Teacher> findByName(String name){
        Teacher teacher=new Teacher();
        teacher.setName(name);
        return teacherRepository.findByName(name);
    }

    /**
     * 通过名字模糊查询教师
     * @param name
     * @return
     */
    public Page<Teacher> findByNameLike(String name,Pageable pageable){
        Teacher teacher=new Teacher();
        teacher.setName(name);
        return teacherRepository.findByNameLike(name, pageable);
    }

    /**
     * 通过名字和密码查询
     * @param name
     * @param password
     * @return
     */
    public List<Teacher> findByNameAndPassword(String name,String password){
        return teacherRepository.findByNameAndPassword(name,password);
    }
}
